package com.resolution.dietaryassessment.view.mydietary.fragment.resultitem;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.style.LineStyle;
import com.bin.david.form.data.table.TableData;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.databeans.CommonConfig;
import com.resolution.dietaryassessment.view.mydietary.surface.ProteinSource;
import com.resolution.dietaryassessment.view.mydietary.surface.SourceEvaluation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author By Resolusion
 * @Title: 蛋白质来源评价
 * @Date: 2020/5/7
 * @Time: 16:29
 * @email Res01ution@foxmail.com
 */
public class ProteinSourceFrag extends Fragment {

    private View view;
    private List<ProteinSource> proteinSourceList = new ArrayList<>();

    public static ProteinSourceFrag newInstance() {
        Bundle args = new Bundle();
        ProteinSourceFrag fragment = new ProteinSourceFrag();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_protein_source, container, false);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            //linearLayout.setBackground(getResources().getDrawable(bundle.getInt("img")));
        }
    }

    private void initView() {
        proteinSourceList.add(new ProteinSource("豆类+鱼肉类(%)",">=30%", CommonConfig.getInstance().getProteinSource()));
        proteinSourceList.add(new ProteinSource("其他类别","<70%",100 - CommonConfig.getInstance().getProteinSource()));

        //普通列
        Column<String> column1 = new Column<>("蛋白质来源", "source");
        Column<Integer> column2 = new Column<>("推荐比例", "mProportion");
        Column<Integer> column3 = new Column<>("我的比例", "myProportionStr");
        Column<String> column4 = new Column<>("评价", "evaluate");

        final TableData<ProteinSource> tableData = new TableData<>("我的蛋白质来源评价",proteinSourceList,column1,column2,column3,column4);
        //设置数据
        SmartTable<ProteinSource> table = view.findViewById(R.id.evaluation_tab);

        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        //表格线style
        table.getConfig().setContentGridStyle(new LineStyle(1,getResources().getColor(R.color.colorDashLine)));
        //列标题表格线
        table.getConfig().setColumnTitleGridStyle(new LineStyle(1,getResources().getColor(R.color.colorDashLine)));
        table.getConfig().setColumnTitleStyle(new FontStyle(getContext(),14,getResources().getColor(R.color.colorMidBlack)));
        table.getConfig().setTableTitleStyle(new FontStyle(getContext(),20,getResources().getColor(R.color.colorMidBlack)));
        table.getConfig().setContentBackground(new BaseBackgroundFormat(getResources().getColor(R.color.colorWeakBlue)));
        table.getConfig().setColumnTitleBackground(new BaseBackgroundFormat(getResources().getColor(R.color.colorWeakOrange)));
        table.setTableData(tableData);
        initPieChart();
    }

    private void initPieChart() {
        PieChart pieChart = view.findViewById(R.id.ps_pie_chart);
        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(proteinSourceList.get(0).getMyProportion(), "豆类+鱼肉类"));
        pieEntries.add(new PieEntry(proteinSourceList.get(1).getMyProportion(), "其他类"));

        pieChart.setDrawEntryLabels(false);
        pieChart.setHoleRadius(0);
        pieChart.setTransparentCircleAlpha(0);
        Description description = new Description();
        description.setTextSize(12);
        description.setText("我的蛋白质来源");
        description.setTextColor(getResources().getColor(R.color.colorBlack));
        pieChart.setDescription(description);

        List<Integer> colors = new ArrayList<>();
        Collections.addAll(colors, Color.parseColor("#feb64d"),
                Color.parseColor("#9287e7"));

        PieDataSet set = new PieDataSet(pieEntries,"");
        set.setColors(colors);

        PieData pieData = new PieData(set);
        pieData.setValueFormatter(new PercentFormatter());
        pieData.setValueTextColor(getResources().getColor(R.color.colorMidBlack));
        pieData.setValueTextSize(10f);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}
